﻿using Lockstep.Collision2D;
using Lockstep.Math;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Lockstep.Game
{
    /// <summary>
    /// 正常直线子弹
    /// </summary>
    public partial class NormalLineBullet : BulletEntity
    {
        List<ColliderProxy> hits = new List<ColliderProxy>();
        public override void DoStart()
        {
            base.DoStart();
            //保持施法者朝向
            transform.deg = caster.transform.deg;
        }

        public override void DoUpdate(LFloat deltaTime)
        {
            base.DoUpdate(deltaTime);
            LVector2 halfDeltaPos = transform.forward * deltaTime * config.normalLine.speed * new LFloat(true, 500);
            //检测碰撞
            hits.Clear();
            PhysicSystem.QueryRegion(config.colliderLayer, (transform.pos + halfDeltaPos), halfDeltaPos, transform.forward ,(ColliderProxy obj) => {
                hits.Add(obj);
            });
            bool isHit = false;
            if (hits.Count > 0)
            {
                isHit = true;
                if (hits[0].Entity is Entity e) {
                    e.TakeDamage(caster, 1, transform.pos);
                }
                else if (hits[0].Entity is MapItemEntity m) {
                    //河流草地不受影响
                    if (m.config.type == SceneObjType.Water || m.config.type == SceneObjType.Grass)
                    {
                        isHit = false;
                    }
                    else { 
                        m.TakeDamage(caster, 1, transform.pos);
                    }
                }
                if (isHit) { 
                    DoDead();
                }
            }
            if(!isHit) 
            {
                transform.pos += transform.forward * deltaTime * config.normalLine.speed;
            }
        }

        public override void DoDestroy()
        {
            base.DoDestroy();
        }
    }
}
